package cn.earas.ip.frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

import cn.earas.ip.Data;
import cn.earas.ip.UpdateCheck;

public class UpdateFrame extends JFrame
{
    private static final long serialVersionUID = 1L;
    private JButton jButton1;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JPanel jPanel1;
    
    public UpdateFrame() {
        this.initComponents();
    }
    
    private void initComponents() {
        this.jPanel1 = new JPanel();
        this.jLabel1 = new JLabel();
        this.jLabel2 = new JLabel();
        this.jLabel3 = new JLabel();
        this.jLabel4 = new JLabel();
        this.jLabel5 = new JLabel();
        this.jButton1 = new JButton();
        this.jLabel6 = new JLabel();
        this.setTitle("更新");
        this.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 97, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 103, 198, 206);
        this.setResizable(false);
        final Toolkit tk = Toolkit.getDefaultToolkit();
        final Image imgae = tk.createImage(this.getClass().getResource("/images/logo.png"));
        this.setIconImage(imgae);
        this.jPanel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)), "\u66f4\u65b0", 0, 0, new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12)));
        this.jPanel1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel1.setText("\u4ee3\u7406IP\u68c0\u6d4b");
        this.jLabel2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel2.setText("\u5f53\u524d\u7248\u672c\uff1a");
        this.jLabel3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel3.setText(Data.VERSION);
        this.jLabel4.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel4.setText("\u6700\u65b0\u7248\u672c\uff1a");
        this.jLabel5.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(final MouseEvent evt) {
                UpdateFrame.this.jLabel5MouseClicked(evt);
            }
        });
        this.jButton1.setText("\u68c0\u67e5\u66f4\u65b0");
        this.jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent evt) {
                UpdateFrame.this.jButton1ActionPerformed(evt);
            }
        });
        this.jLabel6.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", 0, 12));
        this.jLabel6.setText("<html><a href='http://114.67.66.78/ip'>http://114.67.66.78/ip</a></html>");
        this.jLabel6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(final MouseEvent evt) {
                UpdateFrame.this.jLabel6MouseClicked(evt);
            }
        });
        final GroupLayout jPanel1Layout = new GroupLayout(this.jPanel1);
        this.jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGap(66, 66, 66).addComponent(this.jLabel1)).addGroup(jPanel1Layout.createSequentialGroup().addGap(53, 53, 53).addComponent(this.jButton1)).addGroup(jPanel1Layout.createSequentialGroup().addGap(30, 30, 30).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(this.jLabel6, -2, -1, -2).addGroup(jPanel1Layout.createSequentialGroup().addComponent(this.jLabel4).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jLabel5, -2, 50, -2)).addGroup(jPanel1Layout.createSequentialGroup().addComponent(this.jLabel2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jLabel3, -2, 50, -2))))).addContainerGap(28, 32767)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(this.jLabel1).addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel2).addComponent(this.jLabel3)).addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel5, -2, 17, -2).addComponent(this.jLabel4)).addGap(18, 18, 18).addComponent(this.jButton1).addGap(18, 18, 18).addComponent(this.jLabel6, -2, -1, -2).addContainerGap(18, 32767)));
        final GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jPanel1, -2, -1, -2));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jPanel1, GroupLayout.Alignment.TRAILING, -2, -1, -2));
        this.pack();
    }
    
    private void jButton1ActionPerformed(final ActionEvent evt) {
    	if(jLabel5.getText().equals("")){
            String version = UpdateCheck.getLastVersion();
    		if(!version.equals(jLabel3.getText())){
                version = "<html><a href='http://114.67.66.78/download/ipcheck.exe'>"+version+"</a></html>";
            }
            jLabel5.setText(version);
    	}
    }

    private void jLabel5MouseClicked(final MouseEvent evt) {
        if(!jLabel5.getText().equals(jLabel3.getText())){
            try {
                Runtime.getRuntime().exec("cmd.exe /c start http://114.67.66.78/download/ipcheck.exe");
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void jLabel6MouseClicked(final MouseEvent evt) {
        try {
            Runtime.getRuntime().exec("cmd.exe /c start http://114.67.66.78/ip");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
